﻿//======================================================================
//
//        Copyright (C) 2015   
//        All rights reserved
//
//        filename : ReturnCard
//        description : 卡片退货
//        created by XXX at  20150821
//
//======================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CardLogistics.Web.Components;
using System.Data;
using System.Text;

namespace CardLogistics.Web.MainPage.CardRecovery.ReturnCard
{
    public partial class ListInfo : BasePage
    {
        private static readonly CardLogistics.DAL.CardRecovery.CARDRECOVERY_RETURNCARDData dal = new DAL.CardRecovery.CARDRECOVERY_RETURNCARDData();
        protected void Page_Load(object sender, EventArgs e)
        {
            WebUserControl_ToolBar1.DeleteEvent += new EventHandler(WebUserControl_ToolBar1_DeleteEvent);
            if (!IsPostBack)
            {
                //状态 0 未提交 1 待审核 2 审核中 3 驳回 4 审核不通过 5审核通过
                BindDLL(ddlIsSubmit);
                BindDLL(ddlSUPPID, "BASIC_SUPPLIERS", "SUPPLIERSNAME", "SUPPLIERSID", " and STATUS=0 and ISDELETE=0");  //供应商
                BindRpt(LoadStrWhere);
            }
        }

        /// <summary>
        /// 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void WebUserControl_ToolBar1_DeleteEvent(object sender, EventArgs e)
        {
            string delId = this.hdf_Id.Value;
            if (!string.IsNullOrEmpty(delId))
            {
                string[] ids = delId.TrimEnd(',').Split(',');
                int resCount = 0;
                for (int i = 0; i < ids.Length; i++)
                {
                    string resStr = dal.Delete(ids[i]);
                    if (resStr.Split(',')[0] == "ok")
                        resCount++;
                }
                BindRpt("");
            }
        }

        /// <summary>
        /// 绑定Rpt数据
        /// </summary>
        private void BindRpt(string strWhere)
        {
            int recordCount = 0;
            int pageCount = 0;
            StringBuilder sbStr = new StringBuilder();
            sbStr.Append(" 1=1 ");
            if (txt_RETURNCARDNO.Text.Trim().Length > 0)
                sbStr.Append(" and RETURNCARDNO like '%" + txt_RETURNCARDNO.Text.Trim().ToUpper() + "%' ");//退货单号
            if (txt_conno.Text.Trim().Length > 0)
                sbStr.Append(" and BatchNo like '%" + txt_conno.Text.Trim().ToUpper() + "%' ");//合同批次号
            if (ddlSUPPID.SelectedIndex != 0)
                sbStr.Append(" and suppid='" + ddlSUPPID.Value + "'");//供应商
            if (txt_StartRETURNDATA.Text.Trim().Length > 0)
                sbStr.Append(" and to_char(RETURNDATA,'yyyy-MM-dd')>='" + txt_StartRETURNDATA.Text.Trim() + "'");//退货开始日期
            if (txt_EndRETURNDATA.Text.Trim().Length > 0)
                sbStr.Append(" and to_char(RETURNDATA,'yyyy-MM-dd')<='" + txt_EndRETURNDATA.Text.Trim() + "'");//退货结束日期
            //审核状态
            if (ddlIsSubmit.SelectedValue != "-1")
            {
                sbStr.Append(" and IsSubmit=" + ddlIsSubmit.SelectedValue);
            }
            if (!string.IsNullOrEmpty(strWhere))
            {
                sbStr.Append(strWhere);
            }

            DataTable dt = CardLogistics.DAL.CommonData.QuickPage_Oracle("v_cardrecovery_returncard", "ID", " RETURNCARDNO desc", WebPageSize, WebPageIndex, sbStr.ToString(), "*", out pageCount, out recordCount);
            SetNetPagerHtml(AspNetPager1, dt, rptList, recordCount);

            #region 导出数据
            //导出语句
            string strExpSql = "select RETURNCARDNO 退货单号,RETURNDATA 退货日期,ATTN 经办人,conno 合同单号,suppliersname 供应商名称,PRice 单价,DETOTAL 合同数量,returnTotal 退卡数量,REMARKS 备注 from v_cardrecovery_returncard where " + sbStr.ToString() + " order by RETURNCARDNO desc";
            if (!Page.IsClientScriptBlockRegistered("regExportSql"))
                Page.RegisterClientScriptBlock("regExportSql", "<script language=\"Javascript\">var qsql=\"" + Server.UrlEncode(strExpSql) + "\";</script>");
            #endregion
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            WebPageIndex = AspNetPager1.CurrentPageIndex;
            BindRpt("");
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Search_Click(object sender, EventArgs e)
        {
            WebPageIndex = 1;
            BindRpt("");
        }
    }
}